// pages/dailytasks/dailytasks.js
import base from '../../config/base.js';
import Router from '../../utils/router';
import ext from '../../utils/login';
import service from '../../services/phonenumber';
import service_dailytasks from '../../services/dailytasks';
import util from '../../utils/util.js'
import myindexService from '../../services/myindex.js'



Page({

  /**
   * 页面的初始数据
   */
  data: {
    noauthorize: false,
    luck_draw_icon: base.static + 'dailytasks/luckdraw.png',
    sign_icon: base.static + 'dailytasks/sign_icon.png',
    splitpool_icon: base.static + 'dailytasks/splitpool_icon.png',
    step_icon: base.static + 'dailytasks/step_icon.png',
    login_icon: base.static + 'dailytasks/login_icon.png',
    Invitation_icon: base.static + 'dailytasks/Invitation.png',
    placeorder_icon: base.static + 'dailytasks/placeorder.png',
    youhuiquan_bgc: base.static + 'dailytasks/youhuiquanbgc.png',
    couponssmall_bgc: base.static + 'dailytasks/couponssmall.png',
    liulan_icon: base.static + 'dailytasks/liulan.png',
    zhaohuan_icon: base.static + 'dailytasks/jiahaoyou.png',
    publicnumber_icon: base.static + 'dailytasks/gongzhonghao.png',
    share: '',
    order: '',
    browser:'',
    couponslist: [],//优惠券数组
    mask_box: false,//判断显示确认兑换弹框
    exchange_box: false,//判断显示优惠券领取成功弹框
    reduction_num:'',
    conch_num: '',
    time_num: '',//券剩余时间
    full_num: '',
    transmit: {
      style: 'position:absolute;width:100%;height:100%;right:0rpx;top:0rpx;opacity:0;',
      value: ''
    },
    //sdk_flag: false,
    publicNumber: '',
    noauthorize_show:null,
    mask_shouquan:false,
    ewm_flag:false
    
  },
  //去抽奖页
  goluck(){
    let store_id = ''
    try{
      store_id = wx.getStorageSync('user_info').lastView.seid
    }catch(e){}
    Router.go('luck', `store_id=${store_id}`);
  },
  //去签到页面
  goconch() {
    Router.go('conchsignin');
  },
  //去瓜分贝壳页
  goshellForm() {
    Router.go('shellForm');
  }, 
  //去兑换步数页
  gosports() {
    Router.go('sports');
  },
  //去首页
  gostore(){
    Router.go('store');
  },
  //去贝壳专区
  goshellfish(){
    let browsegoods = 'browsegoods';
    Router.go('shellfish',`browsegoods=browsegoods`);   
  },
  //去召唤好友页面
  gomyfirend(){
    Router.go('myfirend');    
  },
  //去关注公众号页面
  goPublicNumber(){
    let that = this;
    if (that.data.user_info.info_authorize == 1) {
      // Router.go('publicnumber')
      that.setData({
        ewm_flag:true
      })
    }else {
      that.setData({
        noauthorize_show: 2,
        mask_shouquan:true
      })
    }
  },
  closemaskbox(){
    this.setData({
      exchange_box: false
    })
  },
  //授权获取用户手机号
  getphone: function (e) {
    let that = this,
      auth_token = wx.getStorageSync('auth_token');
    if (e.detail.iv && e.detail.encryptedData) {
      // 提交手机信息 成功后跳转页面
      let param = {
        encrypted_data: e.detail.encryptedData,
        iv: e.detail.iv,
        auth_token: auth_token
      }
      service.phonenumber(param,function(res){
        if (res && res.code == 1) {
          let phone = res.data.phone;
          let user_info = wx.getStorageSync('user_info');
          user_info.phone = phone;
          wx.setStorageSync('user_info', user_info);
          that.setData({
            noauthorize: false
          })
          wx.showToast({
            title: '已获得100贝壳',
            icon: 'none'
          })
        }
      });
    } 
  },
  
  //获取邀请好友查看和下单状态
  getTaskStatus(){
    let that = this;
    service.getTaskStatus({},function(res){
      if(res && res.code == 1){
        that.setData({
          share: res.data.share,
          order: res.data.order,
          browser: res.data.browser,
          publicNumber: res.data.publicNumber
        })
      }
    })
  },
  //点击立即领取
  getconch(e){
    let that = this;
    if (that.data.user_info.info_authorize == 1) {

    } else {
      that.setData({
        mask_shouquan: true,
        noauthorize_show: 2
      })
      return
    }
    let type_num = e.currentTarget.dataset.type;
    let param = {
      type: type_num
    }
    service.addConch(param,function(res){
      if(res && res.code == 1){
        if (type_num == 20) {
          wx.showToast({
            title: '已获得100贝壳',
            icon: 'none'
          })
        } else {
          wx.showToast({
            title: '已完成',
            icon: 'none'
          })
        }
      }
      that.getTaskStatus();
    })
  },
  //浏览商品领取贝壳
  getsendconch(){
    let that = this;
    if (that.data.user_info.info_authorize == 1) {

    } else {
      that.setData({
        mask_shouquan: true,
        noauthorize_show: 2
      })
      return
    }
    service_dailytasks.sendconch({},function(res){
      if(res && res.code == 1){
        //领取贝壳成功
        wx.showToast({
          title: '领取成功',
          icon: 'none'
        })
      }
      that.getTaskStatus();      
    })
  },
  //获取用户当前贝壳数量
  getconchnnum(){
    myindexService.getconch({},(res)=>{
      if(res && res.code == 1){
        this.setData({
          conch_txt: res.data.conch
        })
      }
    })
  },
  //优惠券列表
  getcouponslist(){
    let that = this;
    service_dailytasks.couponsconchlist({},function(res){
      if (res && res.code == 1) {
        let couponslist = res.data;
        let num = '';
        for (let i = 0; i < couponslist.length; i++){
          couponslist[i].full = util.floatAccMul(couponslist[i].full, 1);
          couponslist[i].reduction = util.floatAccMul(couponslist[i].reduction, 1);
          couponslist[i].progress_num = Math.round((couponslist[i].send_num / couponslist[i].num) * 100); //计算进度百分比
          couponslist[i].width_num = Math.floor((couponslist[i].send_num / couponslist[i].num) * 276); //计算进度条的宽度
        }
        that.setData({
          couponslist: couponslist
        })
      }
    })
  },
  //兑换优惠券弹框
  getcouponsbox(e){
    let reduction_num = e.currentTarget.dataset.reduction;
    let conch_num = e.currentTarget.dataset.conch;
    if (Number(this.data.conch_txt) < Number(conch_num)){
      wx.showToast({
        title: '贝壳不足，无法兑换',
        icon: 'none'
      })
      return;
    }else {
      this.setData({
        mask_box: true,
        reduction_num: reduction_num,
        conch_num: conch_num,
        full_num: e.currentTarget.dataset.full,
        cschid: e.currentTarget.dataset.cschid,
        csid: e.currentTarget.dataset.csid,
        index: e.currentTarget.dataset.index,
        num: e.currentTarget.dataset.num,
      })
    }
    
  },
//取消兑换
  cancleexchange(){
    this.setData({
      mask_box: false
    })
  },
 //立即兑换优惠券
  goexchange(){
   let that = this;
   that.setData({
     mask_box: false,     
   })
  let param = {};
  let inx = that.data.index;
  let strings_proress = "couponslist[" + inx + "].progress_num";
  let strings_width = "couponslist[" + inx + "].width_num";
  let params = {
    cschid: that.data.cschid,
    csid: that.data.csid
  }
   service_dailytasks.getcouponsconch(params,function(res){
     if(res && res.code == 1){
       that.setData({
         exchange_box: true,
         coupons_type: res.data.type,//优惠券id
         time_num: res.data.time,
         send_num: res.data.send_num
       })
       
       param[strings_proress] = Math.round((that.data.send_num / that.data.num) * 100); 
       param[strings_width] = Math.floor((that.data.send_num / that.data.num) * 276); 
        that.setData(param); 
     }else if(res && res.code == 0){
       wx.showToast({
         title: res.data,
         icon: 'none'
       })
     }else if(res && res.code ==2){
       wx.showToast({
         title: '该优惠券已抢光',
         icon: 'none'
       })
       param[strings_proress] = 100;
       that.setData(param); 
     }
   })
 },
 //点击立即使用
  gostoredetail(){
    if (this.data.coupons_type == 0){
      Router.go('store')
    }else {
      let store_id = wx.getStorageSync('user_info').lastView.seid;       
      Router.go('detail', `goods_id=${this.data.coupons_type}&store_id=${store_id}`);              
    }
    this.setData({
      exchange_box: false
    })
  },
  //授权
  onGotUserInfo: function (e) {
    let that = this;
    let d = e.detail;
    if (d.errMsg == 'getUserInfo:ok') {
      ext.getUserInfo(d, b => {
        if (b) {
          let user_info = that.data.user_info
          user_info.info_authorize = 1
          that.setData({
            noauthorize_show: 1,
            mask_shouquan: false,
            user_info: user_info
          })                
        }
      });
    }
  }, 
  closeEwmHandle(){
    this.setData({
      ewm_flag:false
    })
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    let that = this;
    let user_info = wx.getStorageSync("user_info");
    if (user_info.info_authorize == 1 && user_info.phone !=null) {
      
    } else {
      that.setData({
        noauthorize: true,
      })
    }
    that.setData({
      user_info: user_info
    })

    that.getcouponslist();
    that.getconchnnum();
    
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {
   
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    this.getTaskStatus();    
    
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {
  
  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {
  
  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {
  
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
  
  },

})